package com.zhijian.medical.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.po.ExperimentFishReport;
import com.zhijian.medical.entity.vo.request.ExperimentReportSearchVo;
import com.zhijian.medical.entity.vo.request.ExperimentReportUpdatePublicVo;
import com.zhijian.medical.entity.vo.request.ExperimentReportUpdateVo;
import com.zhijian.medical.entity.vo.response.ExperimentReportListVo;
import com.zhijian.medical.entity.vo.response.ExperimentReportPublicVo;
import com.zhijian.medical.entity.vo.response.ExperimentReportVo;
import com.zhijian.medical.entity.vo.response.ExperimentWithTemplateVo;
import com.zhijian.medical.enums.ExperimentTypeEnum;
import com.zhijian.medical.service.ExperimentReportService;
import com.zhijian.medical.service.FishReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 实验报告 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-07-15 17:05:10
 */
@Slf4j
@Controller
@Api(tags = "实验报告")
public class ExperimentReportController {

    @Autowired
    private ExperimentReportService service;

    @Autowired
    private FishReportService fishReportService;

    /**
     * 修改
     *
     * @param id
     * @param reportUpdatePublicVo
     * @return
     */
    @Response
    @PutMapping("/experiment/report/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody ExperimentReportUpdatePublicVo reportUpdatePublicVo) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, reportUpdatePublicVo);

        String s = JSON.toJSONString(reportUpdatePublicVo.getData());
        if (reportUpdatePublicVo.getMainType() != null && reportUpdatePublicVo.getMainType() == ExperimentTypeEnum.FISH.getCode().byteValue()) {
            ExperimentFishReport param = JSON.parseObject(s, new TypeReference<ExperimentFishReport>() {
            });
            param.setId(id);
            return fishReportService.update(param);
        }
        ExperimentReportUpdateVo param = JSON.parseObject(s, new TypeReference<ExperimentReportUpdateVo>() {
        });
        param.setId(id);
        return service.update(param);
    }


    /**
     * 获取最新的数据
     *
     * @return
     */
    @Response
    @GetMapping("/experiment/report/newest")
    @ApiOperation("获取最新的数据")
    public ExperimentReportPublicVo selectById(@RequestParam(required = false) Byte mainType) {
        if (mainType != null && mainType == ExperimentTypeEnum.FISH.getCode().byteValue()) {
            return ExperimentReportPublicVo.builder().mainType(mainType).data(fishReportService.newest()).build();
        }
        return ExperimentReportPublicVo.builder().mainType(mainType).data(service.selectNewestVo()).build();

    }

    /**
     * 查询获取数据
     *
     * @param experimentReportSearchVo
     * @return
     */
    @Response
    @PostMapping("/experiment/report/search")
    @ApiOperation("查询获取数据（弃用）")
    @Deprecated
    public ExperimentReportVo selectBySearch(@RequestBody ExperimentReportSearchVo experimentReportSearchVo) {
        return service.selectNewestVoBySearch(experimentReportSearchVo);
    }

    /**
     * 查询获取列表数据
     *
     * @param experimentReportSearchVo
     * @return
     */
    @Response
    @PostMapping("/experiment/report/list/search")
    @ApiOperation("查询获取列表数据")
    public List<ExperimentReportListVo> selectListBySearch(@RequestBody ExperimentReportSearchVo experimentReportSearchVo) {
        if (experimentReportSearchVo.getMainType() != null && experimentReportSearchVo.getMainType() == ExperimentTypeEnum.FISH.getCode().byteValue()) {
            return fishReportService.list(experimentReportSearchVo);
        }
        return service.selectExperimentReportListVoBySearch(experimentReportSearchVo);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/experiment/report/{id}")
    @ApiOperation("根据id查询")
    public ExperimentReportPublicVo selectById(@PathVariable("id") Long id, @RequestParam(required = false) Byte mainType) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        if (mainType != null && mainType == ExperimentTypeEnum.FISH.getCode().byteValue()) {
            return ExperimentReportPublicVo.builder().data(fishReportService.detail(id)).mainType(mainType).build();
        }
        return ExperimentReportPublicVo.builder().data(service.selectVoById(id)).mainType(mainType).build();
    }

    /**
     * 获取带数据的实验模板
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/experiment/report/with/template/{id}")
    @ApiOperation("获取带数据的实验模板")
    public ExperimentWithTemplateVo selectExperimentWithTemplateVoById(@PathVariable("id") Long id, @RequestParam(required = false) Byte mainType) {
        return service.selectExperimentWithTemplateVoById(id);
    }

}